local zuhuo = fk.CreateSkill {

  name = "fk__zuhuo",

  tags = { Skill.Compulsory, },

}

local U = require "packages/utility/utility"


zuhuo:addEffect(fk.TargetSpecifying, {
  name = "fk__zuhuo",
  mute = true,
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if event == fk.DrawNCards then
      return target == player and player:hasSkill(zuhuo.name)
    else
      return target ~= player and player:hasSkill(zuhuo.name) and player.kingdom == target.kingdom and player:getHandcardNum() > player.hp
      and data.card.is_damage_card and data.to.seat == 1
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(zuhuo.name)
    if event == fk.DrawNCards then
      room:notifySkillInvoked(player, zuhuo.name, "drawcard")
      data.n = data.n + #table.filter(room.alive_players, function (p)
        return p.kingdom == player.kingdom
      end)
    else
      room:notifySkillInvoked(player, zuhuo.name, "negative")
      if not table.contains(data:getAllTargets(), player) then
        data:addTarget(player)
        room:doIndicate(data.from, {player.id})
      end
      data.extra_data = data.extra_data or {}
      data.extra_data.fk__zuhuo = data.extra_data.fk__zuhuo or {}
      table.insert(data.extra_data.fk__zuhuo, player.id)
    end
  end,
})
zuhuo:addEffect(fk.DrawNCards, {
  name = "fk__zuhuo",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.TargetSpecifying, fk.DrawNCards},
  can_trigger = function(self, event, target, player, data)
    if event == fk.DrawNCards then
      return target == player and player:hasSkill(zuhuo.name)
    else
      return target ~= player and player:hasSkill(zuhuo.name) and player.kingdom == target.kingdom and player:getHandcardNum() > player.hp
      and data.card.is_damage_card and data.to.seat == 1
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(zuhuo.name)
    if event == fk.DrawNCards then
      room:notifySkillInvoked(player, zuhuo.name, "drawcard")
      data.n = data.n + #table.filter(room.alive_players, function (p)
        return p.kingdom == player.kingdom
      end)
    else
      room:notifySkillInvoked(player, zuhuo.name, "negative")
      if not table.contains(AimGroup:getAllTargets(data.tos), player.id) and U.canTransferTarget (player, data, false) then
        data:addTarget(player)
        room:sendLog{ type = "#AddTargetsBySkill", from = data.from, to = {player.id}, arg = zuhuo.name, arg2 = data.card:toLogString() }
        room:doIndicate(data.from, {player.id})
      end
      data.extra_data = data.extra_data or {}
      data.extra_data.fk__zuhuo = data.extra_data.fk__zuhuo or {}
      table.insert(data.extra_data.fk__zuhuo, player.id)
    end
  end,
})

zuhuo:addEffect(fk.DamageInflicted, {
  name = "#fk__zuhuo_delay",
  anim_type = "negative",
  frequency = Skill.Compulsory,
  events = {fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    if target == player then
      local e = player.room.logic:getCurrentEvent():findParent(GameEvent.CardEffect)
      if e then
        local use = e.data
        return use.extra_data and table.contains(use.extra_data.fk__zuhuo or {}, player.id)
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    data.damage = data.damage + 1
  end,
})

zuhuo:addEffect("maxcards", {
  name = "#fk__zuhuo_maxcards",
  correct_func = function(self, player)
    if player:hasSkill(zuhuo.name) then
      return #table.filter(Fk:currentRoom().alive_players, function (p)
        return p.kingdom == player.kingdom
      end)
    end
  end,
})

return zuhuo